﻿//---------------------------------------------------------------------------
// 
// File: HtmlTokenType.cs
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
// Description: Definition of token types supported by HtmlLexicalAnalyzer
//
//---------------------------------------------------------------------------

namespace HTMLConverter.XamlConverters
{
    using System.Xml.Linq;
    // DependencyProperty
    // TextElement
    using HTMLConverter.Xml;
    using System.Collections.Generic;

    /// <summary>
    /// HtmlToXamlConverter is a static class that takes an HTML string
    /// and converts it into XAML
    /// </summary>
    internal class SectionConverter : IConverter
    {
        public Tag Type
        {
            get { return Tag.Section; }
        }

        public XmlElement Convert(XNode htmlNode)
        {
            XmlElement section = new XmlElement(HtmlConverter.Xaml_Span, HtmlConverter.Xaml_Namespace);

            if (htmlNode is XmlElement)
            {
                XmlElement htmlElement = htmlNode as XmlElement;
                for (XNode htmlChildNode = htmlElement.FirstNode; htmlChildNode != null; htmlChildNode = htmlChildNode.NextNode)
                {
                    if (htmlChildNode is XmlElement)
                    {
                        XmlElement htmlChildElement = (XmlElement)htmlChildNode;
                        string htmlChildName = htmlChildElement.LowerName;
                        // Switch to an appropriate kind of processing depending on html element name
                        switch (htmlChildName)
                        {
                            case "body":
                            case "form":
                            case "blockquote":
                            case "pre":
                            case "div":
                                section.AppendChild(Converters.Collection[Tag.Section].Convert(htmlChildElement));
                                break;

                            case "a":
                                section.AppendChild(Converters.Collection[Tag.Hyperlink].Convert(htmlChildElement));
                                break;

                            case "img":
                                section.AppendChild(Converters.Collection[Tag.Image].Convert(htmlChildElement));
                                break;

                            case "b":
                            case "strong":
                            case "em":
                            case "dfn":
                            case "code":
                            case "samp":
                            case "var":
                            case "cite":
                            case "kbd":
                                section.AppendChild(Converters.Collection[Tag.Bold].Convert(htmlChildElement));
                                break;

                            case "font":
                                section.AppendChild(Converters.Collection[Tag.Run].Convert(htmlChildElement));
                                break;

                            case "h1":
                            case "h2":
                            case "h3":
                            case "h4":
                            case "h5":
                            case "h6":
                                section.AppendChild(Converters.Collection[Tag.Title].Convert(htmlElement.FirstNode));
                                break;

                            case "ol":
                            case "ul":
                                // List element conversion
                                section.AppendChild(Converters.Collection[Tag.List].Convert(htmlChildElement));
                                break;

                            case "table":
                                section.AppendChild(Converters.Collection[Tag.Table].Convert(htmlChildElement));
                                break;

                            default:
                                section.AppendChild(Converters.Collection[Tag.Span].Convert(htmlChildElement));
                                break;
                        }
                    }
                    else if (htmlChildNode is XmlText)
                    {
                        XmlText htmlChildText = htmlChildNode as XmlText;
                        if (string.IsNullOrEmpty(htmlChildText.Value.Trim()) == false)
                        {
                            section.AppendChild(HtmlConverter.CteateText(htmlChildText.Value));
                        }
                    }
                }
            }
            else if (htmlNode is XmlText)
            {
                XmlText htmlText = htmlNode as XmlText;
                if (string.IsNullOrEmpty(htmlText.Value.Trim()) == false)
                {
                    section.AppendChild(HtmlConverter.CteateText(htmlText.Value));
                }
            }

            return section;
        }
    }
}
